<?

require_once '../includes/specs_functions.php';
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();

#-------------------------------$Session information
$CICUID = $_POST["CICUID"];
$THEME = $_POST["THEME"];
#---------------------------------------------------

$user = $_POST['user'];
$slevel = $_POST['slevel'];
$elevel = $_POST['elevel'];
$job = $_POST['job'];
$amount = $_POST['amount'];
$filter = $_POST['filter'];
$direction = $_POST['direction'];

if($amount == ""){
	$amount = 12;
}
if($direction == ""){
	$direction  = "DESC";
}
if($filter == ""){
	$orderBy = "spec.".SPEC_TBL_SPECS_UDATE;
}else{
	if($filter == "rate"){
		$orderBy = "(rate.value/rate.count)";
	}else{
		$orderBy = $filter;
	}
}
$output = "
		<script type=\"text/javascript\" src=\"../../scripts/tooltip/tooltip.js\"></script>
		<script>		
			var filterDir = '".$direction."';
			
			
			$(document).ready(function(){

				$('.show-spec').click(function(e) {
					var tr = $(this).closest('tr');
					var specId = tr.attr('id');
					editSpec(specId);
				});
				
				$('.show-user').click(function(e) {	
					var clicked = $(this);
					var id = clicked.attr('id');
					getUser(id);
				});
				
				$('.filter-col').click(function(e) {	
					var clicked = jQuery(e.target);
					var id = clicked.attr('id');
					if(filterDir == 'DESC'){
						filterDir = 'ASC'
					}else{
						filterDir = 'DESC'
					}
					query(".$amount.", id, filterDir);
				});
				
				$('.more-button').click(function(e) {	
					query(".($amount+10).", '".$filter."', '".$direction."');
				});
				
				showArrow();
			});
			
			function showArrow(){
				$('.arrow').hide();
				
				var paramsUp = {
						'width': '0',
						'height': '0',
						'border-left': '5px solid transparent',
						'border-right': '5px solid transparent',
						'border-bottom': '5px solid #0368ad'
				}
				var paramsDown = {
						'width': '0',
						'height': '0',
						'border-left': '5px solid transparent',
						'border-right': '5px solid transparent',
						'border-top': '5px solid #0368ad'
				}
				if(filterDir == 'DESC'){
					var params = paramsDown;
				}else{
					var params = paramsUp;
				}
				if ('".$filter."' == ''){
					$('#a_spec_udate').css(params);
					$('#a_spec_udate').fadeIn('fast');
				}else{
					var fil = '".$filter."'
					fil = '#a_'+fil.replace('.','_');
					$(fil).css(params);
					$(fil).fadeIn('fast');
				}
				
			}
			
			$(function(){
				$('.ratings').children().not(':radio').hide();
				$('.ratings').stars({
					disabled: true
				});
			});
		</script>
		<table id='me_specs' class='content-subtitle-table'>";
		
$table = "(((
			".SPEC_TBL_SPECS." as spec 
			LEFT JOIN ".TBL_CHARACTERS." as user
			ON user.".TBL_CHARACTERS_CICUID." = spec.".SPEC_TBL_SPECS_USER_ID.")
			LEFT JOIN (SELECT ".SPEC_TBL_RATINGS_SPEC_ID.", sum(rat.".SPEC_TBL_RATINGS_VALUE.") as value, count(rat.".SPEC_TBL_RATINGS_ID.") as count FROM ".SPEC_TBL_RATINGS." as rat group by rat.".SPEC_TBL_RATINGS_SPEC_ID.") as rate
			ON spec.".SPEC_TBL_SPECS_ID." = rate.".SPEC_TBL_RATINGS_SPEC_ID.")
			LEFT JOIN ".SPEC_TBL_JOBS." as job
			ON job.".SPEC_TBL_JOBS_ID." = spec.".SPEC_TBL_SPECS_JOB_ID.")";
			
$col = array("spec.".SPEC_TBL_SPECS_ID." as specId"
				   ,"spec.".SPEC_TBL_SPECS_USER_ID." as userId"
				   ,"spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
				   ,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
				   ,"spec.".SPEC_TBL_SPECS_LEVEL." as level"
				   ,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
				   ,"spec.".SPEC_TBL_SPECS_PUBLIC." as public"
				   ,"spec.".SPEC_TBL_SPECS_CDATE." as cdate"
				   ,"DATE_FORMAT(spec.".SPEC_TBL_SPECS_UDATE.",'%b %d %Y') as udate"
				   ,"user.".TBL_CHARACTERS_NAME." as userName"
				   ,"user.".TBL_CHARACTERS_AVATAR." as Avatar"
				   ,"rate.value"
				   ,"rate.count"
				);
				   
$where = array("spec.".SPEC_TBL_SPECS_PUBLIC." = 'Y' "
					, "spec.".SPEC_TBL_SPECS_LEVEL." >= \"".$slevel."\""
					,	"spec.".SPEC_TBL_SPECS_LEVEL." <= \"".$elevel."\"");
					
$limit = array(0, ($amount+1));
					
if($_POST['user'] != ""){
	//$abfrage .= " AND user.userName = \"".$user."\"";
	array_push($where, "user.".TBL_CHARACTERS_NAME." like \"%".$user."%\"");
}	
if($_POST['job'] != "none" && $_POST['job'] != ""){
		//$abfrage .= " AND job.name = \"".$job."\"";
		array_push($where, "job.".SPEC_TBL_JOBS_NAME." = \"".$job."\"");
}
/*$abfrage .= $orderBy;	
$abfrage .= " LIMIT 0, ".($amount+1);*/

//$res = mysql_query($abfrage);

$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction, $limit);

$num = $result[0];

//if ($res){
		
	//$num = mysql_num_rows($res);	
	$i = 1;
	if($num > 0){
		$output .= "<tr class='tr-table border-bottom'> 
						<td class='td-table vertical-middle' style='padding-bottom:5px;'>
							<div id='userName' class='filter-col normal-text bold-text blue-text cur-point fl-left' style='overflow: hidden;height: 15px;margin-left: 10px;' >User</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_userName' class='arrow'></div></div>
						</td>
						<td class='td-table vertical-middle'  style='padding-bottom:5px;'>
							<div id='spec.level' class='filter-col normal-text bold-text blue-text cur-point fl-left' >Level</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_spec_level' class='arrow'></div></div>
						</td>
						<td class='td-table vertical-middle' style='padding-bottom:5px;'>
							<div id='job.name' class='filter-col normal-text bold-text blue-text cur-point fl-left'  >Class</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_job_name' class='arrow'></div></div>
						</td>
						<td class='td-table vertical-middle ' style='padding-bottom:5px;'>
							<div id='spec.description' class='filter-col normal-text bold-text blue-text cur-point fl-left' >Description</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_spec_description' class='arrow'></div></div>
						</td>
						<td class='td-table vertical-middle' style='padding-bottom:5px;'>
							<div id='rate' class='filter-col normal-text bold-text blue-text cur-point fl-left' style=''>Rating</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_rate' class='arrow'></div></div>
						</td>
						<td  class='td-table vertical-middle' name='no' style='padding-bottom:5px;'>
							<div id='spec.udate' class='filter-col normal-text bold-text blue-text cur-point fl-left'>Activity</div>
							<div class='fl-left' style='padding-top:5px;margin-left: 10px;'><div id='a_spec_udate' class='arrow'></div></div>
						</td>
					</tr>";
	}else{
		$output .= "<tr class='tr-table'> 
						<td class='td-table vertical-middle pad-5 pad-b-10' colspan='6'> 
							<div class='normal-text' >No specs found</div>
						</td>
					</tr>";
	}

	//while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {

	foreach ($result[3] as $row) {

		$jobName = GetClass($row['jobId']);
	
		$uName = $row['userName'];
		//$nameParts = explode(" ", $uName);
		//$uName = $nameParts[0]." ".substr($nameParts[1], 0, 1).".";
	
		$output .= "<tr id='".$row['specId']."' class='spec-row-hover tr-table";
					if($i != $amount && $i!=$num){
						$output .= " border-bottom ";
					}
					
					$count = $row['count'];
					$value = $row['value'];
					if($count != 0){
						$average = floor($value / $count);
					}else{
						$count = 0;
						$average = 0;
					}
			$output .= "' name='".$row['specId']."'>
						<td class='vertical-middle cur-point'>
							<div style='height:15px;'>
								<div id='".$row['userId']."' class='show-user normal-text blue-text' style='overflow: hidden;height: 15px;width: 100px;margin-left: 10px;'><span class='with-tooltip' title=\"<img src='".str_replace(" ", "", $row['Avatar'])."'></img>\" >".$uName."</span></div>
							</div>
						</td>
						<td class='show-spec vertical-middle cur-point' style='padding-left: 20px'><div class='normal-text' style='margin-right: 24px'>".$row['level']."</div></td>
						<td class='show-spec vertical-middle cur-point'><img style='margin-right: 17px;padding: 3px;' src='".GetClassIcon($jobName)."' /></td>
						<td class='show-spec vertical-middle cur-point'><div class='normal-text  cur-point' style='width: 275px;max-height: 25px;overflow: hidden;padding: 3px;margin-right:13px;'>".$row['description']."</div></td>
						<td class='show-spec vertical-middle cur-point'>
								<form class='ratings' action='' method='post' style='width: 85px;margin-right:13px;' title='Rating: ".$average.", Votes: ".$count."'>";

								foreach (get_options() as $id => $title){
									$output .= 	"<input type='radio' name='rate'  value='".$id."' id='rate".$id."'";
									if($id==$average){
										$output .= 	" checked";
									}
									$output .= 	" />";
								}
						
					$output .= 	"</form>
						</td>
						<td  class='show-spec vertical-middle' name='no'>
							<div class='normal-text' style='width: 80px;'>".$row['udate']."</div>
						</td>
					</tr>";
		if($i == $amount){
			break;
		}
		$i++;
	}
	
	if($i<$num){
		// MORE COMMENTS TO SHOW
		$output .= "<tr>
			<td colspan='6' style='text-align: center;padding-top: 8px;'>
				<div class='more-button cur-point arrow-down center' style='display:inline-block;'/>
			</td>
		</tr>";
		
	}
		

$output .= "</table>
			<div style='clear:both;'></div>";
echo $output; 

#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);

?>